package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.ItemshopComment;
import cn.wolfcode.luowowo.article.mapper.ItemshopCommentMapper;
import cn.wolfcode.luowowo.article.query.ItempShopCommentQuery;
import cn.wolfcode.luowowo.article.service.IItempShopCommentService;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service
public class ItemShopCommentServiceImpl implements IItempShopCommentService {

    @Autowired
    private ItemshopCommentMapper itemshopCommentMapper;

    @Reference
    private IUserInfoService userInfoService;

    @Override
    public void save(UserInfo userInfo, String content, Long toId) {

        ItemshopComment itemshopComment = new ItemshopComment(null,userInfo.getId(),
                userInfo.getNickname(),userInfo.getHeadImgUrl(),userInfo.getLevel(),new Date(),
                content,ItemshopComment.STATUS_NORMAL,0L,null);

        if (toId != -1){
            itemshopComment.setToId(toId);
            UserInfo secondUser = userInfoService.get(toId);
            itemshopComment.setToName(secondUser.getNickname());
            itemshopComment.setStatus(1);

        }

        itemshopCommentMapper.insert(itemshopComment);

    }

    @Override
    public PageInfo query(ItempShopCommentQuery qo) {

        List<ItemshopComment> list = itemshopCommentMapper.query();

        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());

        return new PageInfo(list);
    }
}
